package controller

import (
	"github.com/gin-gonic/gin"
	"main/service"
	"main/tool"
	"strconv"
)

type GoodController struct {
	
}

func (gc *GoodController) Router(r *gin.Engine) {
	r.GET("/api/goods", gc.getGoods)
}

func (gc *GoodController) getGoods(ctx *gin.Context) {
	shopId, ok := ctx.GetQuery("shop_id")
	if !ok {
		tool.Failed(ctx, "请求参数错误,缺少shop_id")
		return
	}

	// 调用service方法
	gs := service.NewGoodService()

	shop_id, err := strconv.Atoi(shopId)
	if err != nil {
		tool.Failed(ctx, "请求参数错误,shop_id不正确")
		return
	}
	goods, err := gs.GetGoods(int64(shop_id))
	if err != nil {
		tool.Failed(ctx, "查询商品为空")
		return
	}
	tool.Success(ctx, goods)
}

